package com.wyl.salary.ui.viewmodel

import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.content.edit
import androidx.lifecycle.AndroidViewModel
import com.wyl.salary.utils.toDate
import java.text.ParseException

class SetDurationViewModel(app: Application) : AndroidViewModel(app) {
    private val sp = app.getSharedPreferences("duration", 0)

    var startDate by mutableStateOf(sp.getString("startDate", "")!!)
    var endDate by mutableStateOf(sp.getString("endDate", "")!!)

    fun save(): Boolean {
        return try {
            startDate.toDate()
            endDate.toDate()
            sp.edit {
                putString("startDate", startDate)
                putString("endDate", endDate)
            }
            true
        } catch (e: ParseException) {
            e.printStackTrace()
            false
        }
    }

    fun onStartDateChanged(date: String) {
        startDate = date
    }

    fun onEndDateChanged(date: String) {
        endDate = date
    }

    fun remove() {
        sp.edit {
            putString("startDate", "")
            putString("endDate", "")
        }
    }
}